package appceshi.action;

import appceshi.service.UserService;
import appceshi.service.impl.UserServiceImpl;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
	private String username;
	private String password;

	private UserService userService = new UserServiceImpl();

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public String execute() throws Exception {
		if (userService.login(username, password)) {
			addActionMessage("Welcome, " + this.username);
			return SUCCESS;
		}

		addActionError("用户名或密码错误");
		return ERROR;
	}

	@Override
	public void validate() {
		if (null == this.username || this.username.isEmpty())
			addFieldError("username", "用户名不能为空");
		if (null == this.password || this.password.isEmpty())
			addFieldError("password", "密码不能为空");
	}
}
